/**
 * Created by PengWei on 2018/7/26.
 */

namespace mengdao {
    export class BaseBundle implements IBundle {
        protected _context: Context;

        public extend(context: Context): void {
            this._context = context;
            this.initialize();
        }

        public initialize(): void {

        }

        protected mapConst(constDefine: { new(...args: any[]): BaseLifecycleObj }): BaseLifecycleObj {
            let constInstance = new constDefine(this._context);
            this._context.injector.bind(constDefine).toConstValue(constInstance);
            return constInstance;
        }

        protected mapDefine(dClass:IClass<BaseLifecycleObj>):void{
            this._context.injector.bind(dClass).toDefine(dClass);
        }

        protected mapCommand(type: string, command: TypeIdentifier<BaseCommand>, once: boolean = false): void {
            this._context.injector.bind(command).toDefine(command);
            this._context.injector.map(type, command, once);
        }

        protected executeCommand(command: IClass<BaseCommand>): void {
            let commandInstance: BaseCommand = new command();
            commandInstance.context = this._context;
            commandInstance.execute();
        }

        protected mapView(viewClass: TypeIdentifier<BaseView<any>>, mediatorClass: TypeIdentifier<BaseMediator>) {
            this._context.injector.bind(mediatorClass).toDefine(mediatorClass);
            this._context.injector.bind(viewClass).toDefine(viewClass);
            this._context.injector.map(viewClass, mediatorClass);
        }
    }
}